version 16
clear all
cd "D:\AGN 2023 RCFS"
set more off
capture log close _all
log using logs\BS.smcl, append name(mainlog)
local lognum = string(clock("$S_DATE $S_TIME","DMY hms"), "%tcCCYY_NN_DD_HH_MM_SS")
log using "logs\sublogs\BSLog_`lognum'.smcl", append name(secondarylog)

	use hmda_data, clear
		
		******************************
		*Appendix Only Code <begin>***
		******************************
			    *This code tests to see if Tables A.7 was conditioned on to be run earlier on
				*If it was, the appropriate Appendix Table (and nothing else) will be generated
				local spapptblind = 0
	
				count if jumbo==1
					if r(N)==0 local spapptblind = 1
				
				if `spapptblind'==0 {
		******************************
		*Appendix Only Code <end>*****
		******************************
		*AGN (2023) Table 1
				reg accept af, robust
				gen summstat_ab_sample  = 1 if e(sample)
			
			su af rat income loanamt if summstat_ab_sample==1, de

			su accept originated white oo conv sing purch if summstat_ab_sample==1
				drop summstat_ab_sample
				
			su taf cdealcount cappcount cvoltotal if istractlevel==1, de	
			su compcount if istractlevel==1 & bankcounter==1, de
			su dealshare if istractlevel==1, de
			
			su ever60dq everdflt	
			su ever60dq everdflt if bbx_data==1
			su ever60dq everdflt if fnma_data==1
			su ever60dq everdflt if fhlmc_data==1
		
		eststo clear
		
			eststo: reg accept af, robust
			
			eststo: reghdfe accept af binbounddist binbounddist_p2 binbounddist_p3, vce(cluster bank) absorb(bankyear)
			
			eststo: reg originate af, robust
			
			eststo: reghdfe originate af binbounddist binbounddist_p2 binbounddist_p3, vce(cluster bank) absorb(bankyear)
			
		esttab using output\AGN_2023_Table2A.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
		
		eststo clear
						
			eststo: reg white af, robust
						
			eststo: reg oo af, robust
						
			eststo: reg conv af, robust
						
			eststo: reg sing af, robust
						
			eststo: reg purch af, robust
						
			eststo: reg jumbo af, robust
						
			eststo: reg rat af, robust
			
		esttab using output\AGN_2023_Table2B.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)

		eststo clear
			
			eststo: reghdfe lcdealcount taf if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				predict caphat_IV if e(sample)

			eststo: reghdfe lfappcount taf if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)

			eststo: ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
				reghdfe tresid_IV caphat_IV if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				drop caphat_IV tresid_IV
				
			eststo: reghdfe lfdealcount taf if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear pdealcount_buckets cappcount_buckets)

			eststo: reghdfe lfvoltotal taf if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear pvoltotal_buckets)
	
		esttab using output\AGN_2023_Table3.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
				
		eststo clear
		
			eststo: reghdfe lfappcount taf tsother if istractlevel==1,  vce(cluster ntract bank) absorb(ntract bankyear compcount cappcount_buckets pappcount_buckets)

			eststo: reghdfe lfappcount taf modtopthree if istractlevel==1, vce(cluster bank ntract) absorb(ntract bankyear compcount cappcount_buckets pappcount_buckets)

			eststo: reghdfe lfappcount taf hhi_diff if istractlevel==1,  vce(cluster bank ntract) absorb(ntract bankyear compcount cappcount_buckets pappcount_buckets)

			eststo: reghdfe lfappcount taf modcompmax if istractlevel==1,  vce(cluster bank ntract) absorb(ntract bankyear compcount cappcount_buckets pappcount_buckets)

			eststo: reghdfe lfappcount modcompmax if istractlevel==1,  vce(cluster bank ntract) absorb(ntract bankyear compcount cappcount_buckets pappcount_buckets)

			eststo: reghdfe lfdealcount taf tsother if istractlevel==1,  vce(cluster ntract bank) absorb(ntract bankyear compcount pdealcount_buckets cappcount_buckets)

			eststo: reghdfe lfdealcount taf modtopthree if istractlevel==1, vce(cluster bank ntract) absorb(ntract bankyear compcount pdealcount_buckets cappcount_buckets)

			eststo: reghdfe lfdealcount taf hhi_diff if istractlevel==1,  vce(cluster bank ntract) absorb(ntract bankyear compcount pdealcount_buckets cappcount_buckets)

			eststo: reghdfe lfdealcount taf modcompmax if istractlevel==1,  vce(cluster bank ntract) absorb(ntract bankyear compcount pdealcount_buckets cappcount_buckets)

			eststo: reghdfe lfdealcount modcompmax if istractlevel==1,  vce(cluster bank ntract) absorb(ntract bankyear compcount pdealcount_buckets cappcount_buckets)

		esttab using output\AGN_2023_Table4.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
		
		eststo clear
			
			eststo: reghdfe ldgmax modcompmax if istractlevel==1 & ldgmax<. & lfappcount<. & modcompmax<.,  vce(cluster bank ntract) absorb(ntract bankyear compcount taf_fe cappcount_buckets pappcount_buckets)
				predict caphat_IV if e(sample)
				
			eststo: reghdfe lfappcount modcompmax if istractlevel==1 & ldgmax<. & lfappcount<. & modcompmax<., vce(cluster bank ntract) absorb(ntract bankyear compcount taf_fe cappcount_buckets pappcount_buckets)

			eststo: ivreghdfe lfappcount (ldgmax=modcompmax) if istractlevel==1 & ldgmax<. & lfappcount<. & modcompmax<., absorb(ntract bankyear compcount taf_fe cappcount_buckets pappcount_buckets, resid(tresid_IV))
				reghdfe tresid_IV caphat_IV if istractlevel==1 & ldgmax<. & lfappcount<. & modcompmax<.,  vce(cluster bank ntract) absorb(ntract bankyear compcount taf_fe cappcount_buckets pappcount_buckets)
				drop tresid_IV
			
			eststo: ivreghdfe lfdealcount (ldgmax=modcompmax) if istractlevel==1 & ldgmax<. & lfdealcount<. & modcompmax<., absorb(ntract bankyear compcount taf_fe pdealcount_buckets cappcount_buckets, resid(tresid_IV))	
				reghdfe tresid_IV caphat_IV if istractlevel==1 & ldgmax<. & lfdealcount<. & modcompmax<.,  vce(cluster bank ntract) absorb(ntract bankyear compcount taf_fe pdealcount_buckets cappcount_buckets)
				drop caphat_IV tresid_IV 
				
			eststo: reghdfe lfdealcount ldgmax if istractlevel==1 & ldgmax<. & lfdealcount<. & modcompmax<.,  vce(cluster bank ntract) absorb(ntract bankyear compcount taf_fe pdealcount_buckets cappcount_buckets)

			eststo: reghdfe lfdealcount firstsecondmax if istractlevel==1,  vce(cluster bank ntract) absorb(ntract bankyear compcount taf_fe pdealcount_buckets cappcount_buckets)

			eststo: reghdfe lfdealcount modcompmax zipmodcompmax  if istractlevel==1 & zip<. & nazip<., vce(cluster bank nazip) absorb(ntract bankyear zcompcount taf_fe pdealcount_buckets cappcount_buckets)

			test modcompmax=zipmodcompmax
			
		esttab using output\AGN_2023_Table5.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
				
		eststo clear
		
			eststo: reghdfe origintrtcalc pmodcompmax ptaf white oo conv sing purch, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe ficoscoreoriginationcalc pmodcompmax ptaf white oo conv sing purch, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe origltvratiocalc pmodcompmax ptaf white oo conv sing purch, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe amortizationtermcalc pmodcompmax ptaf white oo conv sing purch, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe origintrtcalc phhi_diff ptaf white oo conv sing purch, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe ficoscoreoriginationcalc phhi_diff ptaf white oo conv sing purch, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe origltvratiocalc phhi_diff ptaf white oo conv sing purch, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe amortizationtermcalc phhi_diff ptaf white oo conv sing purch, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)
			
		esttab using output\AGN_2023_Table6.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
				
		eststo clear
		
			eststo: reghdfe ever60dq ptaf white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe ever60dq pmodcompmax ptaf white oo conv sing purch if pldgmax<. & ever60dq<. & pmodcompmax<., vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe ever60dq pmodcompmax ptaf white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc if pldgmax<. & ever60dq<. & pmodcompmax<., vce(cluster bank ntract) absorb(ntract bankyear pcompcount)
				reghdfe pldgmax pmodcompmax ptaf white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc if pldgmax<. & ever60dq<. & pmodcompmax<., vce(cluster bank ntract) absorb(ntract bankyear pcompcount)
				predict caphat_IV if e(sample)
				
			eststo: ivreghdfe ever60dq ptaf white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc (pldgmax=pmodcompmax) if pldgmax<. & ever60dq<. & pmodcompmax<., absorb(ntract bankyear pcompcount, resid(tresid_IV))
				reghdfe tresid_IV caphat_IV ptaf white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc if pldgmax<. & ever60dq<. & pmodcompmax<., vce(cluster bank ntract) absorb(ntract bankyear pcompcount)
				drop tresid_IV

			eststo: reghdfe ever60dq ptaf phhi_diff white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe everdflt ptaf white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe everdflt pmodcompmax ptaf white oo conv sing purch if pldgmax<. & ever60dq<. & pmodcompmax<., vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: reghdfe everdflt pmodcompmax ptaf white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc if pldgmax<. & everdflt<. & pmodcompmax<., vce(cluster bank ntract) absorb(ntract bankyear pcompcount)

			eststo: ivreghdfe everdflt ptaf white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc (pldgmax=pmodcompmax) if pldgmax<. & everdflt<. & pmodcompmax<., absorb(ntract bankyear pcompcount, resid(tresid_IV))
				reghdfe tresid_IV caphat_IV ptaf white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc if pldgmax<. & everdflt<. & pmodcompmax<., vce(cluster bank ntract) absorb(ntract bankyear pcompcount)	
				drop caphat_IV tresid_IV
			
			eststo: reghdfe everdflt ptaf phhi_diff white oo conv sing purch origltvratiocalc ficoscoreoriginationcalc origintrtcalc amortizationtermcalc, vce(cluster bank ntract) absorb(ntract bankyear pcompcount)
			
	esttab using output\AGN_2023_Table7.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
	
	******************************
	*Appendix Only Code <begin>***
	******************************
	
			eststo clear
			
				eststo: reghdfe accept af binbounddist binbounddist_p2 binbounddist_p3, vce(cluster bank) absorb(bankyear)

				eststo: reghdfe accept af binbounddist binbounddist_p2 binbounddist_p3 if popularbin==0, vce(cluster bank) absorb(bankyear)

				eststo: reghdfe lfappcount taf if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)

				eststo: reghdfe lfappcount unpopular_taf if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)

				eststo: reghdfe lfappcount taf if istractlevel == 1 & popularappfraction==0,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)

				eststo: reghdfe lfdeniedtoappcountrat taf lpdeniedcount lcdeniedtoappcountrat lpdeniedtoappcountrat if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				
			esttab using output\AGN_2023_AppendixTable2.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
			
			eststo clear
			
				eststo: reghdfe lfappcount taf if istractlevel == 1 & high_app_010_frac==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				
				eststo: reghdfe lfdealcount taf if istractlevel == 1 & high_app_010_frac==1,  vce(cluster bank ntract) absorb(ntractyear bankyear pdealcount_buckets cappcount_buckets)
				
				eststo: reghdfe lfappcount taf if istractlevel == 1 & high_app_025_frac==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				
				eststo: reghdfe lfdealcount taf if istractlevel == 1 & high_app_025_frac==1,  vce(cluster bank ntract) absorb(ntractyear bankyear pdealcount_buckets cappcount_buckets)
				
			esttab using output\AGN_2023_AppendixTable3.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
			
			*AGN (2023) Appendix Table 4
			preserve
			
				keep bank year white oo mrat mrat2 app_010 app_025 aboveonebill
				
				duplicates drop

				gen counter_compbins = 1
				
				collapse (sum) counter_compbins app_010 app_025, by(bank year aboveonebill)
				
					su counter_compbins app_010 app_025, de
					su counter_compbins app_010 app_025 if aboveonebill==1, de
					su counter_compbins app_010 app_025 if aboveonebill==0, de
					su counter_compbins app_010 app_025 if year<=2010, de
					su counter_compbins app_010 app_025 if year>2010, de
			
			restore
			
			eststo clear
		
				eststo: reg accept af if retailbankyear==1, robust
				
				eststo: reg originate af if retailbankyear==1, robust
				
					reghdfe lcdealcount taf if istractlevel == 1 & retailbankyear==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					predict caphat_IV if e(sample)
					reg lcdealcount taf if istractlevel == 1 & ntractyear<. & bankyear<. & cappcount_buckets<. & pappcount_buckets<. & retailbankyear==1
					replace caphat_IV=. if e(sample)==0
				
				eststo: reghdfe lfappcount taf if istractlevel == 1 & retailbankyear==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					reghdfe lfappcount caphat_IV if istractlevel == 1 & retailbankyear==1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				
				eststo:	ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1 & retailbankyear==1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
					reghdfe tresid_IV caphat_IV if istractlevel == 1 & retailbankyear==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					drop caphat_IV tresid_IV
					
				eststo: reghdfe lfdealcount taf if istractlevel == 1 & retailbankyear==1,  vce(cluster bank ntract) absorb(ntractyear bankyear pdealcount_buckets cappcount_buckets)
				
				eststo: reghdfe lfappcount modcompmax_retail if istractlevel==1 & retailbankyear==1,  vce(cluster bank ntract) absorb(ntract bankyear compcount cappcount_buckets pappcount_buckets)
			
			esttab using output\AGN_2023_AppendixTable5.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
			
			
			eststo clear
		
				eststo: reg accept af if nonbank==0, robust
				
				eststo: reg originate af if nonbank==0, robust
				
					reghdfe lcdealcount taf if istractlevel == 1 & nonbank==0,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					predict caphat_IV if e(sample)
					reg lcdealcount taf if istractlevel == 1 & ntractyear<. & bankyear<. & cappcount_buckets<. & pappcount_buckets<. & nonbank==0
					replace caphat_IV=. if e(sample)==0
				
				eststo: reghdfe lfappcount taf if istractlevel == 1 & nonbank==0,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					reghdfe lfappcount caphat_IV if istractlevel == 1 & nonbank==0, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				
				eststo:	ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1 & nonbank==0, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
					reghdfe tresid_IV caphat_IV if istractlevel == 1 & nonbank==0,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					drop caphat_IV tresid_IV
					
				eststo: reghdfe lfdealcount taf if istractlevel == 1 & nonbank==0,  vce(cluster bank ntract) absorb(ntractyear bankyear pdealcount_buckets cappcount_buckets)
				
				eststo: reghdfe lfappcount modcompmax_bank if istractlevel==1 & nonbank==0,  vce(cluster bank ntract) absorb(ntract bankyear compcount cappcount_buckets pappcount_buckets)
			
			esttab using output\AGN_2023_AppendixTable6.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
			
			*Appendix Table 7 is built only if the data is constructed in a particular way
				*Code is below Appendix Table 12

			eststo clear

				eststo: reg accept af if originator_fannie==1, robust
				
				eststo: reg originate af if originator_fannie==1, robust
				
					reghdfe lcdealcount taf if istractlevel == 1 & originator_fannie==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					predict caphat_IV if e(sample) & originator_fannie==1
					reg lcdealcount taf if istractlevel == 1 & ntractyear<. & bankyear<. & cappcount_buckets<. & pappcount_buckets<. & originator_fannie==1
					replace caphat_IV=. if e(sample)==0 & originator_fannie==1
				
				eststo: reghdfe lfappcount taf if istractlevel == 1 & originator_fannie==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					reghdfe lfappcount caphat_IV if istractlevel == 1 & originator_fannie==1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				
				eststo:	ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1 & originator_fannie==1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
					reghdfe tresid_IV caphat_IV if istractlevel == 1 & originator_fannie==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					drop caphat_IV tresid_IV
					
				eststo: reghdfe lfdealcount taf if istractlevel == 1 & originator_fannie==1,  vce(cluster bank ntract) absorb(ntractyear bankyear pdealcount_buckets cappcount_buckets)

			esttab using output\AGN_2023_AppendixTable8.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)

		
			eststo clear

				eststo: reg accept af if originator_freddie==1, robust
				
				eststo: reg originate af if originator_freddie==1, robust
				
					reghdfe lcdealcount taf if istractlevel == 1 & originator_freddie==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					predict caphat_IV if e(sample) & originator_freddie==1
					reg lcdealcount taf if istractlevel == 1 & ntractyear<. & bankyear<. & cappcount_buckets<. & pappcount_buckets<. & originator_freddie==1
					replace caphat_IV=. if e(sample)==0 & originator_freddie==1
				
				eststo: reghdfe lfappcount taf if istractlevel == 1 & originator_freddie==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					reghdfe lfappcount caphat_IV if istractlevel == 1 & originator_freddie==1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				
				eststo:	ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1 & originator_freddie==1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
					reghdfe tresid_IV caphat_IV if istractlevel == 1 & originator_freddie==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					drop caphat_IV tresid_IV
					
				eststo: reghdfe lfdealcount taf if istractlevel == 1 & originator_freddie==1,  vce(cluster bank ntract) absorb(ntractyear bankyear pdealcount_buckets cappcount_buckets)

			esttab using output\AGN_2023_AppendixTable9.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)

		
			eststo clear

				eststo: reg accept af if originator_fannie!=1 & originator_freddie!=1, robust
				
				eststo: reg originate af if originator_fannie!=1 & originator_freddie!=1, robust
				
					reghdfe lcdealcount taf if istractlevel == 1 & originator_fannie!=1 & originator_freddie!=1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					predict caphat_IV if e(sample) & originator_fannie!=1 & originator_freddie!=1
					reg lcdealcount taf if istractlevel == 1 & ntractyear<. & bankyear<. & cappcount_buckets<. & pappcount_buckets<. & originator_fannie!=1 & originator_freddie!=1
					replace caphat_IV=. if e(sample)==0 & originator_fannie!=1 & originator_freddie!=1
				
				eststo: reghdfe lfappcount taf if istractlevel == 1 & originator_fannie!=1 & originator_freddie!=1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					reghdfe lfappcount caphat_IV if istractlevel == 1 & originator_fannie!=1 & originator_freddie!=1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				
				eststo:	ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1 & originator_fannie!=1 & originator_freddie!=1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
					reghdfe tresid_IV caphat_IV if istractlevel == 1 & originator_fannie!=1 & originator_freddie!=1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					drop caphat_IV tresid_IV
					
				eststo: reghdfe lfdealcount taf if istractlevel == 1 & originator_fannie!=1 & originator_freddie!=1,  vce(cluster bank ntract) absorb(ntractyear bankyear pdealcount_buckets cappcount_buckets)

			esttab using output\AGN_2023_AppendixTable10.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)	
			
			
			eststo clear
		
				eststo: reg accept af if year<=2010, robust
				
				eststo: reg originate af if year<=2010, robust
				
					reghdfe lcdealcount taf if istractlevel == 1 & year<=2010,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					predict caphat_IV if e(sample)
					reg lcdealcount taf if istractlevel == 1 & ntractyear<. & bankyear<. & cappcount_buckets<. & pappcount_buckets<. & year<=2010
					replace caphat_IV=. if e(sample)==0
				
				eststo: reghdfe lfappcount taf if istractlevel == 1 & year<=2010,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					reghdfe lfappcount caphat_IV if istractlevel == 1 & year<=2010, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
				
				eststo:	ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1 & year<=2010, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
					reghdfe tresid_IV caphat_IV if istractlevel == 1 & year<=2010,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					drop caphat_IV tresid_IV
					
				eststo: reghdfe lfdealcount taf if istractlevel == 1 & year<=2010,  vce(cluster bank ntract) absorb(ntractyear bankyear pdealcount_buckets cappcount_buckets)
			
			esttab using output\AGN_2023_AppendixTable11.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
			
			eststo clear
					
					reghdfe lcdealcount taf if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					predict caphat_IV if e(sample)
				eststo: ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
					reghdfe tresid_IV caphat_IV if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					drop caphat_IV tresid_IV
					
					reghdfe lcdealcount taf if istractlevel == 1 & aboveonebill==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					predict caphat_IV if e(sample)
				eststo: ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1 & aboveonebill==1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
					reghdfe tresid_IV caphat_IV if istractlevel == 1 & aboveonebill==1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					drop caphat_IV tresid_IV
					
					reghdfe lcdealcount taf if istractlevel == 1 & aboveonebill==0,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					predict caphat_IV if e(sample)
				eststo: ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1 & aboveonebill==0, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
					reghdfe tresid_IV caphat_IV if istractlevel == 1 & aboveonebill==0,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
					drop caphat_IV tresid_IV
				
			esttab using output\AGN_2023_AppendixTable12.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
				
				} 
			*This code tests to see if Table A.7 was conditioned on to be run earlier
			*If it was, the appropriate Appendix Table (and nothing else) will be generated
			count if jumbo==1
			if r(N)==0 {
				
					eststo clear

						eststo: reg accept af, robust
						
						eststo: reg originate af, robust
						
							reghdfe lcdealcount taf if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
							predict caphat_IV if e(sample)
							reg lcdealcount taf if istractlevel == 1 & ntractyear<. & bankyear<. & cappcount_buckets<. & pappcount_buckets<.
							replace caphat_IV=. if e(sample)==0
						
						eststo: reghdfe lfappcount taf if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
							reghdfe lfappcount caphat_IV if istractlevel == 1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
						
						eststo:	ivreghdfe lfappcount (lcdealcount=taf) if istractlevel==1, absorb(ntractyear bankyear cappcount_buckets pappcount_buckets, resid(tresid_IV))
							reghdfe tresid_IV caphat_IV if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear cappcount_buckets pappcount_buckets)
							drop caphat_IV tresid_IV
							
						eststo: reghdfe lfdealcount taf if istractlevel == 1,  vce(cluster bank ntract) absorb(ntractyear bankyear pdealcount_buckets cappcount_buckets)
						
						eststo: reghdfe lfappcount modcompmax if istractlevel==1,  vce(cluster bank ntract) absorb(ntract bankyear compcount cappcount_buckets pappcount_buckets)
				
					esttab using output\AGN_2023_AppendixTable7.tex, r2 ar2 replace starlevels(* 0.10 ** 0.05 *** 0.01)
			}
	******************************
	*Appendix Only Code <end>*****
	******************************